package com.d3works.apps.bpm.service;

import com.d3works.common.ResourceUtils;
import com.d3works.studio.service.loader.AppLoaderImportServiceImpl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.io.IOUtils;

public class AppLoaderImportBpmServiceImpl extends AppLoaderImportServiceImpl {

  private static final String[] IMPORT_FILES =
      new String[] {"wkf-model.xml", "wkf-dmn-model.xml", "baml-model.xml"};

  @Override
  public List<File> getAppImportConfigFiles(File dataDir)
      throws FileNotFoundException, IOException {

    List<File> configFiles = super.getAppImportConfigFiles(dataDir);

    for (String fileName : IMPORT_FILES) {
      String dataFileName = fileName.replace("-call.xml", ".xml");
      if (!(new File(dataDir, dataFileName)).exists()) {
        continue;
      }
      File configFile = new File(dataDir, fileName.replace(".xml", "-config.xml"));
      FileOutputStream fout = new FileOutputStream(configFile);
      InputStream inStream = ResourceUtils.getResourceStream("data-import/" + fileName);
      IOUtils.copy(inStream, fout);
      inStream.close();
      fout.close();
      configFiles.add(configFile);
    }

    return configFiles;
  }
}
